distinctUntilChanged() is a filtering operator, emitting a subset of elements from the upstream flow. In this case, the filtering rule is "eliminate consecutive duplicates".

So, in this case, the 1, 1, 1 portion of our flow will result in a single 1 being emitted downstream, as the duplicates are ignored. Similarly, 2, 2 results in a single 2 being emitted. However, the duplicates have to be consecutive, so the trailing 2 and 1 at the end of the flow are not skipped, despite both 1 and 2 having been in the flow previously.

There is another form of this function, that takes a lambda expression or other function type. That lambda is passed two consecutive values from the flow, and that lambda should return true if those two values are equivalent and the newer one should be ignored.

You can learn more about this in:
Tags:
Run Edit